using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using System;
namespace MicroMsg.Scene
{
	public class NetSceneBindOpMobile : NetSceneBaseEx<BindOpMobileRequest, BindOpMobileResponse, BindOpMobileRequest.Builder>
	{
		private const string TAG = "NetSceneBindOpMobile";
		public const int OPCODE_BINDMOBILE_READY = 1;
		public const int OPCODE_BINDMOBILE_CHECK = 2;
		public const int OPCODE_UNBINDMOBILE = 3;
		public const int OPCODE_BINDMOBILE_CHECKED = 4;
		public static int DIALFLAG_NO = 0;
		public static int DIALFLAG_YES = 1;
		public static string mMobileNumber;
		public static string mTicket;
		public void doSceneBindMobile(string mobile)
		{
			Log.smethod_4("NetSceneBindOpMobile", "bind mobile  (1/2), set mobile =" + mobile);
			NetSceneBindOpMobile.mMobileNumber = mobile;
			this.doSceneEx(mobile, "", 1, NetSceneBindOpMobile.DIALFLAG_NO, "");
		}
		public void doSceneUnBindMobile(string mobile)
		{
			Log.smethod_4("NetSceneBindOpMobile", "unbind mobile  , mobile =" + mobile);
			this.doSceneEx(mobile, "", 3, NetSceneBindOpMobile.DIALFLAG_NO, "");
		}
		public void doSceneVerifyForBindMobile(string verifyCode)
		{
			Log.smethod_4("NetSceneBindOpMobile", "bind mobile  verify (2/2), mobile =" + NetSceneBindOpMobile.mMobileNumber);
			this.doSceneEx(NetSceneBindOpMobile.mMobileNumber, verifyCode, 2, NetSceneBindOpMobile.DIALFLAG_NO, "");
		}
		public void doSceneDialForVerifyCode(string lang)
		{
			Log.smethod_4("NetSceneBindOpMobile", "mobile bind (2/3) ,dial for verify code, lang =" + lang);
			this.doSceneEx(NetSceneBindOpMobile.mMobileNumber, "", 1, NetSceneBindOpMobile.DIALFLAG_YES, lang);
		}
		private void doSceneEx(string mobile, string verifyCode, int opCode, int dialFlag, string dialLang)
		{
			base.beginBuilder();
			string text = AccountMgr.getCurAccount().strUsrName;
			if (text == null)
			{
				text = "";
			}
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.UserName = text;
			this.mBuilder.Mobile = mobile;
			this.mBuilder.Opcode = opCode;
			this.mBuilder.Verifycode = verifyCode;
			this.mBuilder.DialFlag = dialFlag;
			this.mBuilder.DialLang = dialLang;
			this.mSessionPack.mConnectMode = 2;
			this.mSessionPack.mCmdUri = "/cgi-bin/micromsg-bin/bindopmobile";
			base.endBuilder();
		}
		protected override void onSuccess(BindOpMobileRequest request, BindOpMobileResponse response)
		{
			int opcode = request.Opcode;
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (ret == RetConst.MM_OK)
			{
				NetSceneBindOpMobile.mTicket = response.Ticket;
				Log.smethod_5("NetSceneBindOpMobile", string.Concat(new object[]
				{
					"success. opCode =",
					opcode,
					" ticket = ",
					NetSceneBindOpMobile.mTicket
				}));
				if (opcode == 1)
				{
					if (request.DialFlag == NetSceneBindOpMobile.DIALFLAG_YES)
					{
						Log.smethod_4("NetSceneBindOpMobile", "get voice verify code success,phone =" + NetSceneBindOpMobile.mMobileNumber);
						EventCenter.postEvent(EventConst.ON_NETSCENE_MOBILE_VOICEDIAL_SUCCESS, null, null);
						return;
					}
					Log.smethod_5("NetSceneBindOpMobile", "set phone success,need verify, phone = " + NetSceneBindOpMobile.mMobileNumber);
					AccountMgr.getCurAccount().strBindMobile = request.Mobile;
					AccountMgr.MobileVerify = false;
					AccountMgr.updateAccount();
					EventCenter.postEvent(EventConst.ON_NETSCENE_BINDMOBILE_NEEDVERIFY, null, null);
					return;
				}
				else
				{
					if (opcode == 2)
					{
						Log.smethod_5("NetSceneBindOpMobile", "phone " + NetSceneBindOpMobile.mMobileNumber + " verify success");
						AccountMgr.getCurAccount().strBindMobile = request.Mobile;
						AccountMgr.MobileVerify = true;
						AccountMgr.updateAccount();
						EventCenter.postEvent(EventConst.ON_NETSCENE_BINDMOBILE_SUCCESS, null, null);
						return;
					}
					if (opcode == 3)
					{
						Log.smethod_5("NetSceneBindOpMobile", "phone " + NetSceneBindOpMobile.mMobileNumber + " unbind success");
						AccountMgr.getCurAccount().strBindMobile = "";
						AccountMgr.MobileVerify = false;
						AccountMgr.updateAccount();
						EventCenter.postEvent(EventConst.ON_NETSCENE_UNBINDMOBILE_SUCCESS, null, null);
						return;
					}
				}
			}
			else
			{
				Log.smethod_5("NetSceneBindOpMobile", string.Concat(new object[]
				{
					"error in opCode =",
					opcode,
					"  ret = ",
					ret
				}));
				if (opcode == 3)
				{
					EventCenter.postEvent(EventConst.ON_NETSCENE_UNBINDMOBILE_ERR, ret, null);
					return;
				}
				if (request != null && request.DialFlag == NetSceneBindOpMobile.DIALFLAG_YES)
				{
					Log.smethod_4("NetSceneBindOpMobile", "get voice verify code failed,phone =" + NetSceneBindOpMobile.mMobileNumber);
					EventCenter.postEvent(EventConst.ON_NETSCENE_MOBILE_VOICEDIAL_ERR, ret, null);
					return;
				}
				EventCenter.postEvent(EventConst.ON_NETSCENE_BINDMOBILE_ERR, ret, null);
			}
		}
		protected override void onFailed(BindOpMobileRequest request, BindOpMobileResponse response)
		{
			int opcode = request.Opcode;
			if (opcode == 3)
			{
				EventCenter.postEvent(EventConst.ON_NETSCENE_UNBINDMOBILE_ERR, -800000, null);
				return;
			}
			if (request != null && request.DialFlag == NetSceneBindOpMobile.DIALFLAG_YES)
			{
				Log.smethod_4("NetSceneBindOpMobile", "get voice verify code failed,phone =" + NetSceneBindOpMobile.mMobileNumber);
				EventCenter.postEvent(EventConst.ON_NETSCENE_MOBILE_VOICEDIAL_ERR, -800000, null);
				return;
			}
			EventCenter.postEvent(EventConst.ON_NETSCENE_BINDMOBILE_ERR, -800000, null);
		}
		private static void onHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_BINDMOBILE_SUCCESS)
			{
				Log.smethod_5("NetSceneBindOpMobile", "(2/2)on success");
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_BINDMOBILE_NEEDVERIFY)
			{
				Log.smethod_5("NetSceneBindOpMobile", "(1/2)on success");
				string verifyCode = "1234";
				ServiceCenter.sceneBindMobile.doSceneVerifyForBindMobile(verifyCode);
			}
		}
		public static void testBindMobile()
		{
			EventWatcher eventWatcher = new EventWatcher(null, null, new EventHandlerDelegate(NetSceneBindOpMobile.onHandler));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_BINDMOBILE_SUCCESS, eventWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_BINDMOBILE_NEEDVERIFY, eventWatcher);
			ServiceCenter.sceneBindMobile.doSceneBindMobile("13530159751");
		}
		private static void onHandler2(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_UNBINDMOBILE_SUCCESS)
			{
				Log.smethod_5("NetSceneBindOpMobile", "(1/1)on success");
			}
		}
		public static void testUnBindMobile()
		{
			EventWatcher eventWatcher = new EventWatcher(null, null, new EventHandlerDelegate(NetSceneBindOpMobile.onHandler2));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_BINDMOBILE_SUCCESS, eventWatcher);
			ServiceCenter.sceneBindMobile.doSceneUnBindMobile("13530159751");
		}
	}
}
